package com.hk.jmm.app;

import android.content.Intent;
import android.graphics.Color;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.toast.ToastUtils;
import com.hk.jmm.R;
import com.hk.jmm.event.UsbDeviceIn;
import com.hk.jmm.utils.DeviceCache;
import com.luck.picture.lib.utils.SpUtils;
import com.skyz.base.activity.BaseActivity;
import com.skyz.base.util.EventBusUtils;

public class WelcomeActivity extends BaseActivity {
    String log="-";
    @Override
    protected void onMessageEvent(Object event) {

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        log += "onNewIntent\n";
        checkDeive(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        log += "onCreate\n";
        super.onCreate(savedInstanceState);
        checkDeive(getIntent());
    }

    void checkDeive(Intent intent){
        DeviceCache.getInstance().setDevice(null);
        if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
            UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            log += "device="+device.getDeviceName()+"\n";
            DeviceCache.getInstance().setDevice(device);
            EventBusUtils.post(new UsbDeviceIn());
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }
    private final String key = "first";
    TextView tv_agreement,tv_agreement_again;
    @Override
    protected void onLayoutInflated() {
        setLayoutFullScreen(true);
        log += "onLayoutInflated\n";
        boolean isFirst = SpUtils.getBoolean(this,key,true);
        if(!isFirst){
            nextActivity();
            return;
        }


        tv_agreement = findViewById(R.id.tv_agreement);
        tv_agreement_again = findViewById(R.id.tv_agreement_again);

        String str = new StringBuffer().append("为了给您提供优质的使用体验，镜猫猫需联网为您提供相关服务。在使用APP前，请您仔细阅读并同意《用户协议》《隐私政策》了解您的用户权益及相关数据的处理方式。")
                .append("\n")
                .append("为帮助您快速了解我们提供的服务及如何收集您的信息，特向您说明如下:")
                .append("\n")
                .append("1.我们将为您提供绑定智能眼镜、控制智能眼镜等服务，并收集、使用您必要的个人信息!")
                .append("\n")
                .append("2.我们会根据您的使用场景，申请获取您的设备系统权限，系统将通过弹窗征得您的授权，您有权拒绝或撤回这些授权;")
                .append("\n")
                .append("3.我们会采取多方位的安全保护措施以确保您的个人信息安全，除非经过您的同意或相关法律法规规定，我们不会和任何第三方共享您的个人信息。如您同意以上内容，请点击“同意并继续”开始使用我们的产品和服务，感谢您的信任!")
                .append("\n").toString();

        SpannableString spannableString = getSpannableString(str);

        tv_agreement.setMovementMethod(LinkMovementMethod.getInstance());
        tv_agreement.setText(spannableString);

        String str2 = new StringBuffer().append("已阅读并同意《用户协议》《隐私政策》").toString();
        SpannableString spannableString2= getSpannableString(str2);
        tv_agreement_again.setMovementMethod(LinkMovementMethod.getInstance());
        tv_agreement_again.setText(spannableString2);
    }


    private  SpannableString getSpannableString(String str) {
        UnderlineSpan blueSpan = new UnderlineSpan(){
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.parseColor("#4D8DFF"));
                ds.setUnderlineText(false);

            }
        };

        SpannableString spannableString = new SpannableString(str);
        ClickableSpan clickableSpanUserAgreement = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                ToastUtils.show( "用户协议");
            }
        };
        int startA = str.indexOf("《用户协议》");
        spannableString.setSpan(clickableSpanUserAgreement, startA, startA+6, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//        spannableString.setSpan(blueSpan, startA, startA+6, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        ClickableSpan clickableSpanYinsi = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                ToastUtils.show( "隐私政策");
            }
        };
        int startB = str.indexOf("《隐私政策》");
        spannableString.setSpan(clickableSpanYinsi, startB, startB+6, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//        spannableString.setSpan(blueSpan, startB, startB+6, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return spannableString;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
            findViewById(R.id.click_agreent).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    agreent();
                }
            });
        findViewById(R.id.click_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exit();
            }
        });
    }

    private void agreent(){
        //todo 点击同意
        SpUtils.putBoolean(this,key,false);
        LoginAndRegisterActivity.showActivity(WelcomeActivity.this);
        finish();
    }

    private void exit(){
        finish();
    }

    void nextActivity(){
        ToastUtils.show(log);
        MainActivity.showActivity(this);
        finish();
        //todo 1 有用户跳登录 2 没用户跳首页
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                LoginAndRegisterActivity.showActivity(WelcomeActivity.this);
//                finish();
//            }
//        },5000);
    }

}
